const express = require('express');
const connection = require('../mysql') // 获取连接实例
const app = express();
const $sql = require('../db');

//获取所有商品信息
app.get('/gealltdata',(req,res) => {
	var sql = $sql.stock_sql.gealltdata;
	connection.query(sql,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据id获取商品信息
app.post('/geadata',(req,res) => {
	var sql = $sql.stock_sql.getdata;
	var params = req.body.id
	connection.query(sql,params,(err,result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//商品入库
app.post('/setstonum',(req,res) => {
	var sql = $sql.stock_sql.setstonum;
	var params = [req.body.num,req.body.id];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据商品名称查询供应商编号
app.post('/getsupname',(req,res) => {
	var sql = $sql.stock_sql.getstoname;
	var params = [req.body.name];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据商品名称和供应商名称获取仓库名称
app.post('/getwarname',(req,res) => {
	var sql = $sql.stock_sql.getwarname;
	var params = [req.body.name,req.body.supplier_id];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//根据商品名称和供应商名称和仓库名称获商品信息
app.post('/getstoid',(req,res) => {
	var sql = $sql.stock_sql.getstoid;
	var params = [req.body.name,req.body.supplier_id,req.body.wid];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//更新商品信息
app.put('/upstockdata',(req,res) => {
	var sql = $sql.stock_sql.upstockdata;
	var params = [req.body.img,req.body.name,req.body.spe,
			req.body.num,req.body.unit,req.body.wid,
			req.body.alarmnum,req.body.production_date,req.body.expiration_date,
			req.body.supplier_id,req.body.purchase_price,req.body.retail_price,req.body.id];
			console.log(params)
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//删除商品信息
app.delete('/delstockdata',(req,res) => {
	var sql = $sql.stock_sql.delstockdata;
	var params = [req.body.id];
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})
//添加商品
app.put('/stockadd', (req, res) => {
	var sql = $sql.stock_sql.stockadd;
    var params = [req.body.img,req.body.name,req.body.spe,req.body.num,
				  req.body.wid,req.body.alarmnum,req.body.production_date,req.body.expiration_date,
				  req.body.supplier_id,req.body.purchase_price,req.body.retail_price,req.body.unit]
	connection.query(sql,params,(err, result) => {
		if (err) {
			console.log(err)
			return res.send(err);
		} else {
			return res.send(result);
		}
	})
})

module.exports = app;